// pages/home2/home.js
const app = getApp()
var wxParse = require('../../vendor/wxParser/index');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgheights: [],
        scrollWidth: 0,
        currentNavtab: 0,//当前第几个swiper 
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.loadData()
        wx.getSystemInfo({
            success: res => {
                //获取屏幕的宽度并保存
                this.setData({
                    scrollWidth: res.windowWidth
                });
            }
        });
    },
    //等比缩放图片并保存
    imageLoad: function (e) {
        //获取图片真实宽度  
        var imgwidth = e.detail.width,
            imgheight = e.detail.height,
            //宽高比  
            ratio = imgwidth / imgheight;
        //console.log(imgwidth, imgheight);
        //计算的高度值  

        var viewHeight = parseInt(this.data.scrollWidth) / ratio;
        var imgheight = viewHeight.toFixed(0);
        var imgheightarray = this.data.imgheights;
        //把每一张图片的高度记录到数组里
        imgheightarray.push(imgheight);

        this.setData({
            imgheights: imgheightarray,
        });
    },
    swiperChange: function (e) {
        //console.log(e.detail.current);    
        this.setData({
            currentNavtab: e.detail.current
        })
    },
    returnHome() {
        wx.switchTab({
            url: '/pages/find/find',
        })
    },
    loadData() {
        wx.showLoading({
            title: '加载中...',
        })
        app.http.get('company/company-index', {
            data: {
                id: app.storeId
            }
        }).then(res => {
            this.setData({
                banner: res.data.banner,
                companyInfo: res.data.item,
            })
            this.loadCompany()
            wxParse.parse({
                bind: 'content',
                html: res.data.item.content,
                target: this,
                enablePreviewImage: false, // 禁用图片预览功能
            });
            wx.setNavigationBarTitle({
                title: res.data.item.name,
            })
            wx.hideLoading()
        }).catch(err => {
            app.wxToast.error(err.message)
        })
    },
    loadCompany() {
        app.http.get('company/company-card', {
            data: {
                id: app.storeId
            }
        }).then(res => {
            this.setData({
                company: res.data.item
            })
        })
    },
    checkLocation() {
        wx.openLocation({
            latitude: parseFloat(this.data.company.lat),
            longitude: parseFloat(this.data.company.lng),
            name: this.data.company.name,
            address: this.data.company.address
        })
    },
    call() {
        wx.makePhoneCall({
            phoneNumber: this.data.company.mobile,
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})